/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 19182
 * Date: 2025-07-24
 * Time: 17:42
 */
public class Partition {
    public ListNode partition(ListNode pHead, int x) {
        if(pHead == null){
            return null;
        }
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;
        ListNode cur = pHead;
        while(cur != null){
            if(cur.val < x){
                if(bs == null){ //刚开始头尾都为空
                    bs = be = cur;
                }else{
                    be.next = cur;
                    be = cur;
                }
            }else{
                if(as == null){
                    as = ae = cur;
                }else{
                    ae.next = cur;
                    ae = cur;
                }
            }
            cur = cur.next;
        }
        if(bs == null){ //全是大于等于x的节点
            return as;
        }
//        if(as == null){
//            return bs;
//        }
        be.next = as;
//        ae.next = null;
        return bs;
    }

    public static void main(String[] args) {
        ListNode node0 = new ListNode(1);
        ListNode node1 = new ListNode(2);
        ListNode node2 = new ListNode(3);
        node0.next = node1;
        node1.next = node2;
        Partition partition = new Partition();
        ListNode ret = partition.partition(node0, 4);
        System.out.println("hehe");
    }
}
